package com.sh.dc.config;

import com.sh.dc.common.enums.OssTypeEnum;
import com.sh.dc.service.IOSSService;
import com.sh.dc.service.impl.AliOSSService;
import com.sh.dc.service.impl.MinioService;
import io.micrometer.common.util.StringUtils;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;
import java.util.function.Supplier;

@Slf4j
@Data
@Configuration
@ConfigurationProperties(prefix = "oss")
public class OssClientConfig {
    private String type;
    private String endpoint;
    private String bucket;
    private String accessKey;
    private String secretKey;
    private Integer signExpires;

    private final Map<String, Supplier<IOSSService>> ossServiceMap = Map.of(
            OssTypeEnum.MINIO.getCode(), () -> new MinioService(this),
            OssTypeEnum.ALI_OSS.getCode(), () -> new AliOSSService(this)
    );

    @Bean
    public IOSSService ossService() {
        if (StringUtils.isEmpty(type)) {
            throw new IllegalArgumentException("oss.type 不能为空");
        }

        Supplier<IOSSService> supplier = ossServiceMap.get(type);
        if (supplier == null) {
            throw new UnsupportedOperationException("不支持的 OSS 类型: " + type);
        }
        return supplier.get();
    }
}
